package com.example.lishop.service.impl

import com.example.lishop.entity.BBusinessEntity
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.repository.BusinessRepository
import com.example.lishop.service.BusinessService
import com.example.lishop.vo.BusinessDetailVo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.util.Assert

/**
 * Created on 2021/3/14 18:14
 * @author shuguo
 */
@Service
class BusinessServiceImpl : BusinessService{

    @Autowired
    lateinit var repository: BusinessRepository
    @Autowired
    lateinit var service: ProductServiceImpl

    override fun add(data: BBusinessEntity): BBusinessEntity {
        return repository.save(data)
    }

    override fun update(data: BBusinessEntity) {
        detail(data)
        return repository.update(data.depId, data.name, data.descript, data.type, data.tag, data.status, data.sts)
    }

    override fun del(data: BBusinessEntity) {
        detail(data)
        repository.del(data.depId)
    }

    override fun list(page:Int, size:Int): Page<BBusinessEntity> {
        val req: Pageable = PageRequest.of(page, size)
        return repository.findAll(req)
    }

    override fun detail(data: BBusinessEntity): BBusinessEntity? {
        val data = repository.findByDepId(data.depId)
        Assert.notNull(data, ExceptEnum.NOT_EXIST)
        return data
    }

    override fun detailAll(data: BBusinessEntity): BusinessDetailVo {
        val bean: BusinessDetailVo = BusinessDetailVo()
        bean.detail = detail(data)
        bean.products = service.listByBusinessId(data.depId)
        return bean
    }

}